package com.market.cash.utils.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.market.cash.CashRootApps
import com.market.cash.R
import com.market.cash.databinding.CashCommonPopupBinding
import com.market.cash.utils.adapter.CashRootOnItemChildClickListener
import com.market.cash.utils.adapter.CashRootRecyclerViewAdapter
import com.market.cash.utils.adapter.CashRootViewHolderHelper
import com.market.cash.utils.popup.EasyPopup
import com.market.cash.utils.popup.XGravity
import com.market.cash.utils.popup.YGravity
import org.json.JSONObject

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/22 14:46
 * @Annotation：列表选择
 */
class CashRootCommonPopupwindow(val context: Context) {
    lateinit var binding: CashCommonPopupBinding
    //开始选择时间
    private var startTime = 0L;

    /* 筛选框宽度，默认屏幕宽度 */
    var popupWidth = CashRootApps.app.getPhoneWidth()
    //选中的item
    var selectItem: JSONObject? = null

    private var mItemListener: IItemClickListener? = null

    interface IItemClickListener {
        fun onConfirmAction(data: JSONObject?)
        fun onDismissListener()
    }
    private var mEasyPopup: EasyPopup? = null

    //分类
    private var mAdapter: BrandAdapter? = null

    fun show(view: View, dimView: ViewGroup) {
        startTime = System.currentTimeMillis()
        if (mEasyPopup == null) {
            showViewDown(view, dimView)
        } else {
            mEasyPopup!!.showAtAnchorView(view, YGravity.BELOW, XGravity.CENTER, 0, 0)
        }
    }

    /* 选择回调 */
    fun setSelectItemListener(listener: IItemClickListener?) {
        mItemListener = listener
    }

    /* 设置宽度 */
    fun setWindowWidth(width: Int){
        this.popupWidth = width
    }

    /* 设置分类数据 */
    fun setBrandList(list: List<JSONObject>) {
        this.mAdapter!!.data = list
    }

    private fun showViewDown(view: View, dimView: ViewGroup) {
        mEasyPopup = EasyPopup(context)
        binding = CashCommonPopupBinding.inflate(LayoutInflater.from(context))
        mEasyPopup!!.setContentView(binding.root)
        mEasyPopup!!.width = this.popupWidth
//        mEasyPopup!!.height = Apps.getPhoneHeight() / 2
        mEasyPopup!!.setAnimationStyle(R.style.popup_anim)
        //是否允许点击PopupWindow之外的地方消失
        mEasyPopup!!.setFocusAndOutsideEnable(true)
        //允许背景变暗
        mEasyPopup!!.setBackgroundDimEnable(false)
        //变暗的透明度(0-1)，0为完全透明
        mEasyPopup!!.setDimValue(0.4f)
        //变暗的背景颜色
        mEasyPopup!!.setDimColor(Color.BLACK)
        //指定任意 ViewGroup 背景变暗
        mEasyPopup!!.setDimView(dimView)
//        mEasyPopup!!.apply();
        EasyPopup.create()
        mEasyPopup!!.showAtAnchorView(view, YGravity.BELOW, XGravity.CENTER, 0, 0)
        //排序列表
        initRecyclerView()
        mEasyPopup!!.setOnDismissListener { // popupWindow隐藏时恢复屏幕正常透明度
            if (mItemListener != null) {
                mItemListener!!.onDismissListener()
            }
        }
    }

    private fun initRecyclerView() {
        binding.recyclerView.layoutManager = LinearLayoutManager(context)
        this.mAdapter = BrandAdapter( binding.recyclerView)
        binding.recyclerView.adapter = this.mAdapter
        binding.recyclerView.itemAnimator = DefaultItemAnimator()
        this.mAdapter!!.setOnItemChildClickListener(onItemClickListener()) //点击
    }

    /* RecyclerView点击Item */
    internal inner class onItemClickListener : CashRootOnItemChildClickListener {
        @SuppressLint("NotifyDataSetChanged")
        override fun onItemChildClick(parent: ViewGroup, childView: View, position: Int) {
            val model = mAdapter!!.getItem(position)
            if (childView.id == R.id.itemLayout) {
                for (brandItem in mAdapter!!.data) {
                    brandItem.put("isSelect",false)
                    if (brandItem!!.optString("title") == model.optString("title")) {
                        brandItem.put("isSelect",true)
                    }
                }
                mAdapter!!.notifyDataSetChanged()
                if (mItemListener != null) {
                    model.put("duration",System.currentTimeMillis() - startTime)
                    mItemListener!!.onConfirmAction(model)
                    mEasyPopup!!.dismiss()
                }
            }
        }
    }

    internal inner class BrandAdapter(recyclerView: RecyclerView?) : CashRootRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.cash_common_popup_item) {
        override fun setItemChildListener(helper: CashRootViewHolderHelper, viewType: Int) {
            helper.setItemChildClickListener(R.id.itemLayout) //点击Item
        }

        override fun fillData(helper: CashRootViewHolderHelper, position: Int, model: JSONObject) {
            val textTitle = helper.getTextView(R.id.textTitle)
            textTitle.text = model.optString("title")

            val itemLayout = helper.getView<LinearLayout>(R.id.itemLayout)
            if (model.optBoolean("isSelect",false)) {
                helper.setVisibility(R.id.imgSelect,View.VISIBLE)
                itemLayout.setBackgroundColor(Color.parseColor("#30435CFA"))
            } else {
                helper.setVisibility(R.id.imgSelect,View.INVISIBLE)
                itemLayout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white))
            }
            if (position == mData.size - 1){
                helper.setVisibility(R.id.v_line,View.INVISIBLE)
            } else {
                helper.setVisibility(R.id.v_line,View.INVISIBLE)
            }
        }
    }
}